package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Web;
import cn.stylefeng.guns.modular.system.service.IWebService;

/**
 * body内容控制器
 *
 * @author fengshuonan
 * @Date 2020-07-17 21:37:08
 */
@Controller
@RequestMapping("/web")
public class WebController extends BaseController {

    private String PREFIX = "/system/web/";

    @Autowired
    private IWebService webService;

    /**
     * 跳转到body内容首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "web.html";
    }

    /**
     * 跳转到添加body内容
     */
    @RequestMapping("/web_add")
    public String webAdd() {
        return PREFIX + "web_add.html";
    }

    /**
     * 跳转到修改body内容
     */
    @RequestMapping("/web_update/{webId}")
    public String webUpdate(@PathVariable Integer webId, Model model) {
        Web web = webService.selectById(webId);
        model.addAttribute("item",web);
        LogObjectHolder.me().set(web);
        return PREFIX + "web_edit.html";
    }

    /**
     * 获取body内容列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<Web> entityWrapper = new EntityWrapper<>();
        Wrapper<Web> like =  entityWrapper.like("id",condition)
                .or().like("url",condition)
                .or().like("region",condition).orderBy("id",false);
        return webService.selectList(like);
    }

    /**
     * 新增body内容
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Web web) {
        webService.insert(web);
        return SUCCESS_TIP;
    }

    /**
     * 删除body内容
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer webId) {
        webService.deleteById(webId);
        return SUCCESS_TIP;
    }

    /**
     * 修改body内容
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Web web) {
        webService.updateById(web);
        return SUCCESS_TIP;
    }

    /**
     * body内容详情
     */
    @RequestMapping(value = "/detail/{webId}")
    @ResponseBody
    public Object detail(@PathVariable("webId") Integer webId) {
        return webService.selectById(webId);
    }
}
